package com.notebad.ui;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.notebad.GeneralVariables;
import com.notebad.R;
import com.notebad.note.Note;
import com.notebad.ui.preferences.PreferencesView;

/**
 *	Main apps Activity. This activity is loading first (see AndroidManifest.xml file).
 *
 */
public class WelcomeView extends Activity {

	@SuppressWarnings("unused")
	private static final String TAG = "WelcomeView";
	private static final int PREF_ID = Menu.FIRST;

	private ImageView editStartButton;
	private ImageView listStartButton;
	private ImageView imgStartButton;
	private ImageView recordStartButton;
	private LinearLayout bgLayout;
	private Drawable bgDrawable;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		editStartButton = (ImageView) findViewById(R.id.StartEdit);
		listStartButton = (ImageView) findViewById(R.id.StartList);
		imgStartButton = (ImageView) findViewById(R.id.StartImg);
		recordStartButton = (ImageView) findViewById(R.id.StartRecord);

		bgLayout = (LinearLayout)findViewById(R.id.welcomeViewBG);
		bgDrawable = this.getResources().getDrawable(R.drawable.ic_bg_welcome);
		bgLayout.setBackgroundDrawable(bgDrawable);

		//Create application directory if it does not exist
		File notebadFolder = new File(GeneralVariables.getApplicationFolder());
		if(!notebadFolder.exists() || !notebadFolder.isDirectory()){
			notebadFolder.mkdir();
		}

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				switch(view.getId()) {
				case R.id.StartEdit:
					Note.initCurrentNote();
					Intent textView = new Intent(WelcomeView.this, VisualisationView.class);
					textView.putExtra("tab", 0);
					startActivity(textView);
					return;
				case R.id.StartRecord:
					Note.initCurrentNote();
					Intent audio = new Intent(WelcomeView.this, AudioRecorderView.class);
					audio.putExtra("fromMenu", true);
					startActivity(audio);
					return;
				case R.id.StartImg:
					Note.initCurrentNote();
					Intent cam = new Intent(WelcomeView.this, CameraView.class);
					cam.putExtra("fromMenu", true);
					startActivity(cam);
					return;
				case R.id.StartList:
					Intent list = new Intent(WelcomeView.this, NoteListView.class);
					startActivity(list);
					return;
				}
			}
		};

		editStartButton.setOnClickListener(listener);
		listStartButton.setOnClickListener(listener);
		imgStartButton.setOnClickListener(listener);
		recordStartButton.setOnClickListener(listener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, PREF_ID, 0, R.string.menu_preferences).setIcon(R.drawable.ic_menu_preferences);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case PREF_ID:
			startActivity(new Intent(this, PreferencesView.class));
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}